package com.justgame.project.service.system.service;

import com.justgame.project.base.utils.OrderUtil;
import com.justgame.project.cloud.common.constant.Const;
import com.justgame.project.cloud.common.exception.Exc;
import com.justgame.project.cloud.common.util.ObjUtil;
import com.justgame.project.cloud.common.util.ShiroUtils;
import com.justgame.project.service.api.feign.IJustGameInfoClient;
import com.justgame.project.cloud.common.entity.PageRequest;
import com.justgame.project.service.api.vo.JustGameInfoVO;
import com.justgame.project.service.api.dto.OnlineUserInfoDTO;
import com.justgame.project.service.api.dto.SysStationOrderDTO;
import com.justgame.project.service.api.entity.OnlineUserInfo;
import com.justgame.project.service.api.feign.IOnlineUserClient;
import com.justgame.project.service.system.mapper.SysStationOrderMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Map;

@Service
@Transactional
public class SysStationOrderService {

    @Resource
    private IOnlineUserClient onlineClient;

    @Resource
    private SysStationOrderMapper stationOrderMapper;

    @Resource
    private IJustGameInfoClient justGameInfoClient;

    public PageRequest<?> getPurchaseList(PageRequest<Map<String, Object>> pageRequest) {
        String userId = ShiroUtils.getUserId();
        if (StringUtils.isBlank(userId)) {
            throw new Exc("请先登录后再重试！");
        }
        pageRequest.startPage();
        return pageRequest.BuildPageInfo(stationOrderMapper.getPurchaseList(userId));
    }

    public Boolean buildOrder(String gameId) {
        String userId = ShiroUtils.getUserId();
        if (StringUtils.isBlank(userId)) {
            throw new Exc("请先登录后再重试！");
        }
        JustGameInfoVO gameInfo = justGameInfoClient.selectById(gameId);

        if (gameInfo == null || gameInfo.getId() == null) {
            throw new Exc("因该游戏不存在，所以暂无法购买！");
        }

        if (stationOrderMapper.hasBought(userId, gameId)) {
            throw new Exc("您已购买过此游戏，请勿重复购买哦！");
        }

        // 价格安全格式化 ( 如果价格为空或者小于0 则置为0 )
        gameInfo.setCost(gameInfo.getCost() == null || gameInfo.getCost() < 0 ? 0 : gameInfo.getCost());

        // 创建订单对象
        SysStationOrderDTO dto = new SysStationOrderDTO();
        dto.setGameId(gameId);
        dto.setOrderCode(OrderUtil.buildStationOrderCode());
        dto.setRemark(Const.REMARK_GAME_ORDER);
        ObjUtil.inject(dto, true);
        /* 查询用户信息 */
        OnlineUserInfo onlineInfo = onlineClient.getOnlineInfo(userId);
        if (onlineInfo == null) {
            throw new Exc("账户信息有误，请联系管理人员！");
        }

        dto.setBeforeCoins(onlineInfo.getJCoins());
        dto.setBeforeIntegral(onlineInfo.getOnlineIntegral());

        /* 有效期3年 */
        dto.setExpireDate(LocalDateTime.now().plus(3, ChronoUnit.YEARS));
        /* 设置消费额度 */
        dto.setCostCoins(gameInfo.getCost());

        //TODO 这里还差判断资源是否是VIP 以及判断用户是否是VIP
        //if (gameInfo.getIsVip() != null && gameInfo.getIsVip() == 1 && )
        if (gameInfo.getCost() > onlineInfo.getJCoins()) {
            throw new Exc("您的JCoins余额不足，因此无法完成支付！");
        }
        stationOrderMapper.buildOrder(dto);

        /* 更新账户余额 */
        OnlineUserInfoDTO onlineDto = new OnlineUserInfoDTO();
        onlineDto.setJCoins(onlineInfo.getJCoins() - gameInfo.getCost());
        onlineDto.setOnlineId(onlineInfo.getOnlineId());

        return onlineClient.updateById(onlineDto);
    }
}
